package com.example.yunhailutong.zhongtan.activity;


import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatDelegate;
import android.view.KeyEvent;
import android.widget.Toast;

import com.example.yunhailutong.zhongtan.Adapter.ViewPageAdapter;
import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.app.OldsEvent;
import com.yinglan.alphatabs.AlphaTabsIndicator;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MainActivity extends ActivityBase {

    //填充MainActivity
    @BindView(R.id.view_pager)
    ViewPager viewPager;

    //底部滑动选项按钮
    @BindView(R.id.alphaIndicator)
    AlphaTabsIndicator alphaIndicator;

    private ViewPageAdapter adapter;
    private long mExitTime;


    @Override
    protected void addListener() {

    }

    @Override
    protected void initActivity(Bundle savedInstanceState) {
        initUI();
        EventBus.getDefault().register(this);
        //Bugly.init(getApplicationContext(), "ee7fabc23c", false);
    }

    @Override
    protected int contentViewById() {
        boolean enabledTranslucentNavigation = getSharedPreferences("shared", Context.MODE_PRIVATE)
                .getBoolean("translucentNavigation", false);

        return R.layout.activity_main;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void exit() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            Toast.makeText(MainActivity.this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            mExitTime = System.currentTimeMillis();
        } else {
            finish();
            System.exit(0);
        }
    }

    private void initUI() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
        }
        viewPager =findViewById(R.id.view_pager);
        viewPager.setOffscreenPageLimit(5);

        alphaIndicator = findViewById(R.id.alphaIndicator);
        //关联Fragment
        adapter = new ViewPageAdapter(getSupportFragmentManager());
        viewPager.setAdapter(adapter);
        alphaIndicator.setViewPager(viewPager);//设置ViewPager

    }

    /**
     * Reload activity
     */
    public void reload() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    //登录失效
    @Subscribe
    public void TokenShiXiao(OldsEvent.TokenShiXiaoEvent msg) {
        disPlay("不好意思您的登录已经失效请重新登录！");
    }
    @Subscribe
    public void changeViewPager(OldsEvent.ChangeViewPagerEvent msg) {
        viewPager.setCurrentItem(1);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
